package com.zygh.lz.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zygh.lz.entity.Equipage;
import com.zygh.lz.entity.query.EquipageQuery;
import com.zygh.lz.entity.vo.EquipageVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface EquipageMapper extends BaseMapper<Equipage> {

    //有则更新，无则插入
    int insertIgnoreNull(Equipage equipage);

    //根据警号查询最后一个坐标信息
    Equipage findEquipageEnd(String code);

    //在线设备集合
    List<Map<String, Object>> findFacilityLocaltion(@Param("sectionName") String sectionName,
                                                    @Param("type") String type);

    //在线数量
    int findFacilityCount(@Param("sectionName") String sectionName,
                          @Param("type") String type);

    //警车累计在线数量
    int findFacilityUp(@Param("sectionName") String sectionName,
                       @Param("type") String type);

    //警车+车累计在线数量
    int findFacilityTotal(@Param("sectionName") String sectionName,
                          @Param("type") String type);

    //根据警号查询
    @Select("SELECT equipage.id, equipage.gpsid, type, car_no carNo, se.police_number warningSignal, dept_name deptName\n" +
            "FROM equipage\n" +
            "         left join staff_equipage se on equipage.gpsid = se.gpsid and se.is_del = 0\n" +
            "WHERE se.police_number = #{code} \n" +
            "  AND (qrcode != '')")
    List<Map<String, Object>> getEquipageById(String code);
//
//    List<Map<String, Object>> findLocationById(Integer id);

    @Update("UPDATE equipage SET warning_signal = #{code} WHERE gpsid = #{gpsid}")
    int updateEquipage(Equipage equipage);

    //查询在线的警车，upload_time在5分钟内
    @Select("select id,gpsid,longitude,latitude,car_no carNo,type,dept_name deptName,upload_time from equipage " +
            " where id = #{id} " +
            " and TIMESTAMPDIFF(MINUTE,upload_time,NOW()) <= 10 and type in (select type from equipage_type where type_name='警车') ")
    Map<String, Object> findOnLine(Integer id);

    //查询全部警车
    @Select("<script>" +
            "select id,car_no carNo,type,dept_name deptName from equipage " +
            "<where> " +
            " <if test='carNo != null and carNo.length &gt;= 0'> " +
            " AND (car_no LIKE CONCAT('%',#{carNo},'%') OR dept_name LIKE" +
            " CONCAT('%',#{carNo},'%'))" +
            " </if> " +
            " and type in (select type from equipage_type where type_name='警车')" +
            " </where> " +
            "</script>")
    List<Map<String, Object>> getByCarNo(String carNo);

    //查询不在线的警车，upload_time不在5分钟内
    @Select("select id,gpsid,longitude,latitude,car_no carNo,type,dept_name deptName,upload_time from equipage " +
            " where id = #{id} " +
            " and TIMESTAMPDIFF(MINUTE,upload_time,NOW()) > 5 and type in (select type from equipage_type where type_name='警车') ")
    Map<String, Object> findNotOnLine(Integer id);

    int policeEquipageBind(@Param("equipage") Equipage equipage);

    @Select("select id,gpsid,type,car_no carNo,warning_signal warningSignal,dept_name deptName" +
            " from equipage where qrcode = #{qrcode} LIMIT 1;")
    Equipage getEquipageByQrcode(@Param("qrcode") String qrcode);

    EquipageVO getEquipageVOByQrcode(@Param("qrcode") String qrcode);

    @Update("update equipage set warning_signal = null where gpsid = #{gpsid}")
    void unbindingEquipment(String gpsid);

    List<Long> getAutoUnbindingEquipageList();

    /**
     * 根据条件筛选装备数据
     * @param equipageQuery 装备筛选条件
     * @return java.util.List<com.zygh.lz.entity.vo.EquipageVO>
     * @author jhoal
     * @description 根据条件筛选装备数据
     * @date 14:38 2023/6/6
     **/
    List<EquipageVO> getEquipmentList(@Param("equipageQuery") EquipageQuery equipageQuery);
}
